<?php
/**
 * 自动创建时间
 * @property mixed $attribute string or array
 * @property string $format date format for PHP function date 
 * @author yangdongqi <yangdongqi@hayzone.com>
 * @package lib.behaviors.auto
 */
class CCreateTime extends CActiveRecordBehavior {
	/**
	 * model所对应的字段名
	 * @var mixed
	 */
	public $attribute = 'create_time';
	/**
	 * 时间格式
	 * @var string
	 */
	public $format = 'Y-m-d H:i:s';
	
	public function beforeValidate($event) {
		if($this->owner->isNewRecord) {
			if(is_array($this->attribute)) {
				foreach($this->attribute as $one) {
					if ($this->owner->hasAttribute($one))
						$this->owner->{$one} = date($this->format);
				}
			} else {
				if ($this->owner->hasAttribute($this->attribute))
					$this->owner->{$this->attribute} = date($this->format);
			}
		}
	}
	
}

?>